package com.tm.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tm.ResultEntity;
import com.tm.mapper.CommonMapper;
import com.tm.mapper.EcStoreManageMapper;
import com.tm.mapper.EcTypeInfoMapper;
import com.tm.mapper.TtHeadlinesInfoMapper;
import com.tm.model.EcStoreManage;
import com.tm.model.EcTypeInfo;
import com.tm.model.TtHeadlinesInfo;
import com.tm.util.DateUtilsEx;


/**
 * APP通用管理接口服务类
 * 
 * @author GXL
 *
 */
@Service
public class CommonService {

	@Autowired
	private CommonMapper cMapper;
	
	@Autowired
	private EcTypeInfoMapper etiMapper;
	
	@Autowired
	private EcStoreManageMapper esmMapper;
	
	@Autowired
	private TtHeadlinesInfoMapper thiMapper;
	
	
	/**
	 * 根据区域名称获取区域详情
	 * @param name
	 * @param id
	 * @param lat 纬度
	 * @param lng 经度
	 * @param distance 门店距离范围
	 * @return
	 */
	public ResultEntity getEcAreaByParam(String name, String id, String lat, String lng, String distance) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotEmpty(name))
				param.put("name", name);
			if(StringUtils.isNotEmpty(id))
				param.put("id", id);
			
			Map<String, Object> area = cMapper.getEcAreaByName(param);
			
			//根据当前经纬度以及距离范围获取最近门店，若找不到附近门店，前端页面则跳转选择门店页面
			param.put("distance", distance);
			param.put("lng", new BigDecimal(lng));
			param.put("lat", new BigDecimal(lat));
			List<Map<String, Object>> list = esmMapper.getEcStoreByDistance(param);
			Map<String, Object> store = null;
			if(list.size() > 0){
				store = list.get(0);
			}
			
			area.put("store", store);
			
			re = new ResultEntity(area);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	
	/**
	 * 根据上级区域id获取下级区域列表
	 * @param areaId 上级区域ID
	 * @return
	 */
	public ResultEntity getAreaByParentAreaId(Integer areaId) {
		ResultEntity re = null;
		
		try{
			
			List<Map<String, Object>> list = cMapper.getAreaByParentAreaId(areaId);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**

	/**
	 * 搜索银行支行
	 * @param city 城市名称
	 * @param bankNo 银行编码
	 * @param branch 支行名称
	 * @return
	 */
	public ResultEntity getBankBranch(String city, String bankNo, String branch) {
		ResultEntity re = null;
		
		try{
			
			List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("city", "%" + city + "%");
			param.put("bankNo", bankNo);
			param.put("branch", "%"+ branch + "%");
			
			list = cMapper.getBankBranch(param);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 新增附件
	 * @param attach
	 * @return
	 */
	public int addAttach(Map<String, Object> attach) {
//		String aname = attach.get("filename");
		attach.put("uploadTime", DateUtilsEx.formatToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
		attach.put("attach_id", 0);
		
//		cMapper.addAttach(attach);
		
		/*if(re > 0){
			Map<String, Object> res = cMapper.getAttachByName(aname);
			if(res != null){
				re = Integer.parseInt(res.get("attach_id").toString());
			}else 
				re = 0;
		}*/
		
		return (int) attach.get("attach_id");
	}


	/**
	 * 获取首页商品分类
	 * @return
	 */
	public List<EcTypeInfo> getEcTypeInfoForIndex() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startIndex", 0);
		param.put("pageSize", 5);
		return etiMapper.getEcTypeInfoList(param);
	}


	/**
	 * 获取所有商品分类
	 * @return
	 */
	public List<EcTypeInfo> getEcTypeInfoList() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startIndex", 0);
		param.put("pageSize", 1000);
		return etiMapper.getEcTypeInfoList(param);
	}


	/**
	 * 获取分类详情
	 * @param typeId
	 * @return
	 */
	public EcTypeInfo getEcTypeInfoByTypeId(Integer typeId) {
		return etiMapper.selectByPrimaryKey(typeId);
	}


	/**
	 * 根据广告位编码获取广告信息
	 * @param adno
	 * @return
	 */
	public List<Map<String, Object>> getAdvertisementByAdno(String adno) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		
		list = cMapper.getAdvertisementByAdno(adno);
		
		return list;
	}


	/**
	 * 获取资讯列表
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public ResultEntity getTtHeadlinesList(Integer startIndex, Integer pageSize) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("startIndex", startIndex);
			param.put("pageSize", pageSize);
			
			List<Map<String, Object>> list = thiMapper.getTtHeadlinesListByPage(param);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}


	/**
	 * 资讯详情
	 * @param infoId
	 * @return
	 */
	public TtHeadlinesInfo getTtHeadlinesById(Integer infoId) {
		return thiMapper.selectByPrimaryKey(infoId);
	}


	public Long getSecs(String tm) {
		return cMapper.getSecs(tm);
	}


	/**
	 * 
	 * @param typeNo
	 * @return
	 */
	public List<Map<String, Object>> getSysCodeByTypeNo(String typeNo) {
		return cMapper.getSysCode(typeNo);
	}

}
